import 'package:efood_multivendor/controller/filter_controller.dart';
import 'package:efood_multivendor/controller/restaurant_controller.dart';
import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/images.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/custom_button.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:range_slider_flutter/range_slider_flutter.dart';

class FilterWidget extends StatelessWidget {
  final double maxValue;
  final bool isRestaurant;

  FilterWidget({required this.maxValue, required this.isRestaurant});

  @override
  Widget build(BuildContext context) {
    final LinearGradient linearGradient = LinearGradient(
      colors: <Color>[
        Theme.of(context).primaryColor,
        Theme.of(context).colorScheme.secondary
      ],
    );

    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.vertical(top: Radius.circular(8.0)),
        color: Theme.of(context).cardColor,
      ),
      padding: EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
      child: GetBuilder<FilterController>(builder: (filterController) {
        return SingleChildScrollView(
          child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  width: 100.w,
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        InkWell(
                          onTap: () => Navigator.of(context).pop(),
                          child: Padding(
                            padding: EdgeInsets.all(
                                Dimensions.PADDING_SIZE_EXTRA_SMALL),
                            child: Icon(Icons.close,
                                color: Theme.of(context).disabledColor),
                          ),
                        ),
                        Text('sort_and_filter'.tr,
                            style: sfMedium.copyWith(
                                fontSize: Dimensions.fontSizeLarge * 1.2)),
                        CustomButton(
                          onPressed: () {
                            filterController.clearFilters();
                          },
                          buttonText: 'clear_all'.tr,
                          transparent: true,
                          width: 26.w,
                        ),
                      ]),
                ),
                SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
                Text(
                  "price_range".tr,
                  style: sfRegular.copyWith(
                      fontSize: Dimensions.fontSizeLarge * 1.15),
                ),
                SizedBox(
                  height: 4.h,
                ),
                Padding(
                  padding: EdgeInsets.only(right: 8.w, left: 5.w),
                  child: RangeSliderFlutter(
                      values: [
                        filterController.selpriceLower ?? 0,
                        filterController.selpriceHigher ?? 0
                      ],
                      step: RangeSliderFlutterStep(step: 10),
                      rangeSlider: true,
                      tooltip: RangeSliderFlutterTooltip(
                        boxStyle: RangeSliderFlutterTooltipBox(
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(17),
                                gradient: linearGradient)),
                        alwaysShowTooltip: true,
                        leftSuffix: Text(' Br',
                            style: TextStyle(
                              fontSize: 15,
                              color: Colors.white,
                            )),
                        rightSuffix: Text(' Br',
                            style: TextStyle(
                              fontSize: 15,
                              color: Colors.white,
                            )),
                      ),
                      max: (Get.find<SplashController>()
                                  .configModel!
                                  .maximumProductPrice
                                  ?.toDouble() ??
                              0) +
                          1000,
                      textPositionTop: -70,
                      handlerHeight: 25,
                      trackBar: RangeSliderFlutterTrackBar(
                        activeTrackBarHeight: 10,
                        inactiveTrackBarHeight: 10,
                        activeTrackBar: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          gradient: linearGradient,
                        ),
                        inactiveTrackBar: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color:
                              Theme.of(context).primaryColor.withOpacity(0.3),
                        ),
                      ),
                      min: 0,
                      fontSize: 15,
                      textColor: Colors.white,
                      textBackgroundColor: Colors.red,
                      onDragging: (handlerIndex, lowerValue, upperValue) {
                        filterController.setPrice(lowerValue, upperValue);
                      }),
                ),
                Text('sort'.tr,
                    style: sfMedium.copyWith(
                        fontSize: Dimensions.fontSizeLarge * 1.3)),
                SizedBox(height: Dimensions.PADDING_SIZE_SMALL),
                Padding(
                  padding: EdgeInsets.only(right: 5.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            "cost_low_to_high".tr,
                            style: sfRegular.copyWith(
                                fontSize: Dimensions.fontSizeDefault * 1.2),
                          ),
                          GestureDetector(
                            onTap: () {
                              if (filterController.selectedIndex ==
                                  SortMode.CostAsc) {
                                filterController
                                    .setSelectedIndex(SortMode.Distance);
                              } else {
                                filterController
                                    .setSelectedIndex(SortMode.CostAsc);
                              }
                            },
                            child: Image.asset(filterController.selectedIndex ==
                                    SortMode.CostAsc
                                ? Images.checkboxTicked
                                : Images.checkboxOutlined),
                          )
                        ],
                      ),
                      SizedBox(
                        height: 3.h,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            "cost_high_to_low".tr,
                            style: sfRegular.copyWith(
                                fontSize: Dimensions.fontSizeDefault * 1.2),
                          ),
                          GestureDetector(
                              onTap: () {
                                if (filterController.selectedIndex ==
                                    SortMode.CostDesc) {
                                  filterController
                                      .setSelectedIndex(SortMode.Distance);
                                } else {
                                  filterController
                                      .setSelectedIndex(SortMode.CostDesc);
                                }
                              },
                              child: Image.asset(
                                  filterController.selectedIndex ==
                                          SortMode.CostDesc
                                      ? Images.checkboxTicked
                                      : Images.checkboxOutlined))
                        ],
                      ),
                      SizedBox(
                        height: 3.h,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            "rating".tr,
                            style: sfRegular.copyWith(
                                fontSize: Dimensions.fontSizeDefault * 1.2),
                          ),
                          GestureDetector(
                              onTap: () {
                                if (kDebugMode) {}
                                if (filterController.selectedIndex ==
                                    SortMode.Rating) {
                                  filterController
                                      .setSelectedIndex(SortMode.Distance);
                                } else {
                                  filterController
                                      .setSelectedIndex(SortMode.Rating);
                                }
                              },
                              child: Image.asset(
                                  filterController.selectedIndex ==
                                          SortMode.Rating
                                      ? Images.checkboxTicked
                                      : Images.checkboxOutlined))
                        ],
                      ),
                      SizedBox(
                        height: 4.h,
                      ),
                    ],
                  ),
                ),
                SizedBox(height: 30),
                CustomButton(
                  buttonText: 'apply'.tr,
                  radius: 15,
                  height: 6.h,
                  onPressed: () {
                    filterController.setFilters();
                    Get.find<RestaurantController>()
                        .getRestaurantList('1', true);

                    Navigator.of(context).pop();
                  },
                ),
              ]),
        );
      }),
    );
  }
}
